package cn.smbms.controller.user;

import cn.smbms.tools.HttpClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/wx")
public class WXController {
    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> login(String code){

        Map<String,Object> map=new HashMap<String,Object>();
        //1、请求的准备工作，拿到appid,appsecret,前端传来的code，拼接在请求微信后台的接口里
        //小程序的appid
        String appid="wx74dadd0f83575df4";
        //小程序的密钥
        String appsecret="b6bb9f4d764b011be9c655f7e6c2b72f";
        String url="https://api.weixin.qq.com/sns/jscode2session?"
        +"appid="+appid+"&secret="+appsecret+"&js_code="+code+"&grant_type=authorization_code";
        //发送请求
        String responseData=HttpClientUtil.doGet(url);
        System.out.println(responseData);
        //生成返回前端的结果
        if(responseData.contains("errcode")){
            map.put("status",500);
        }
        map.put("status",200);
        map.put("ticket", UUID.randomUUID());
        return map;
    }
}
